package service;

import dao.VegetableSelectDao;
import dao.VegetableUpdataDao;
import dao.VegetablequeryDao;
import entity.Vegetable;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class updateService {

    public static void main(String[] args) throws SQLException {
        VegetableUpdataDao vegetableUpdataDao = new VegetableUpdataDao();
//        VegetablequeryDao vegetablequeryDao = new VegetablequeryDao();
        VegetableSelectDao vegetableSelectDao = new VegetableSelectDao(); //用于整理表中现有数据数量
        System.out.println("请输入要进行修改内容的蔬菜名称");
        Scanner scanner = new Scanner(System.in);
        String vname = scanner.nextLine();
//        List<Vegetable> vegetableList = new ArrayList<Vegetable>();
        List<Vegetable> vegetableList1 = new ArrayList<Vegetable>();

//        vegetableList=vegetablequeryDao.querybyName(vname);  //用于查找特定的列，给特定的列赋值
//        vegetableList1=vegetableSelectDao.findAll();
//        System.out.println(vegetableList);


        System.out.println("(格式为yyyy-MM-dd)请输入种植日期:");
        Date date = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

            date = new Date(formatter.parse(scanner.nextLine()).getTime());
            new Date(date.getTime());
        } catch (ParseException e) {
            System.out.println("日期格式输入错误");
            return;
        }
        System.out.println("请输入数量:");
        int num = scanner.nextInt();
        System.out.println("请输入蔬菜单价:");
        BigDecimal price = scanner.nextBigDecimal();
        System.out.println("请输入蔬菜状态(0/1)");
        int status = scanner.nextInt();
        Vegetable vegetable = new Vegetable();

        vegetable.setVegename(vname);
        vegetable.setPlanttime(date);
        vegetable.setNumber(num);
        vegetable.setPrice(price);
        vegetable.setVstatus(status);
//        for (int i = 0; i < vegetableList1.size(); i++) {
//            if (vegetableList1.get(i).getVegename().equals(vname)) {
//                vegetable.setPlanttime(date);
//                vegetable.setNumber(num);
//                vegetable.setPrice(price);
//                vegetable.setVstatus(status);
//            }
//        }



            if (vegetableUpdataDao.updataVegetable(vegetable)){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }

//        Date finalDate = date;
//         vegetableList.stream()
//                .map((vegetable) -> {
//                    if (vegetable.getVegename().equals(vname)) {
//
//                        Vegetable vegetables = new Vegetable();
//
//                        vegetables.setPlanttime(finalDate);
//                        vegetables.setNumber(num);
//                        vegetables.setPrice(price);
//                        vegetables.setVstatus(status);
//
//                        try {
//                            if (vegetableUpdataDao.updataVegetable(vegetables)) {
//                                System.out.println("修改成功");
//                            } else {
//                                System.out.println("修改失败");
//                            }
//                        } catch (SQLException e) {
//                            throw new RuntimeException(e);
//                        }
//
//                        return vegetables;
//                    }
//                    return vegetable;
//                }).toList();
//        for (int i = 0; i < vegetableList.size(); i++) {
//            if(vegetableList.get(i).getVegename().equals(vname)){
//
//
//                        Vegetable vegetable = new Vegetable();
//                        System.out.println("请输入种植日期:");
//
//                        Date date = null;
//                        try {
//                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//
//                            date = new Date(formatter.parse(scanner.nextLine()).getTime());
//                            new Date(date.getTime());
//                        } catch (ParseException e) {
//                            System.out.println("日期格式输入错误");
//                            return;
//                        }
//
//                vegetable.setPlanttime(vegetab.getPlanttime());
//                        System.out.println("请输入数量:");
//                        vegetable.setNumber(item.getNumber());
//                        System.out.println("请输入蔬菜名称:");
//                        vegetable.setVegename(item.getVegename());
//                        System.out.println("请输入蔬菜单价:");
//                        vegetable.setPrice(item.getPrice());
//
//        }
//        System.out.println(vegetableList);
//    }

    }
}
